﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Kooboo.Commerce.Promotions.DiscountByCategory
{
    internal class Context
    {
        public Context()
        {
            this.DataContext = new DiscountDataContext(Kooboo.Commerce.Configuration.FindConnection());
        }

        public DiscountDataContext DataContext
        {
            get;
            set;
        }

        public Promotion_DiscountByCategory Find(IPromotionRule rule)
        {
            return DataContext.Promotion_DiscountByCategories.Where(d => d.DiscountRuleId == rule.Id).FirstOrDefault();
        }

        public Promotion_DiscountByCategory Find(Int32 ruleId)
        {
            return DataContext.Promotion_DiscountByCategories.Where(d => d.DiscountRuleId == ruleId).FirstOrDefault();
        }

        public void Remove(IPromotionRule rule)
        {
            var items = DataContext.Promotion_DiscountByCategories.Where(i => i.DiscountRuleId == rule.Id);

            DataContext.Promotion_DiscountByCategories.DeleteAllOnSubmit(items);

            DataContext.SubmitChanges();
        }
    }
}